frameworks.widgets.Result = function(code){
	this.valid = false;
	this.data = "";
	this.errors = {};
	this.attachment = [];
	this.source = "";
	this.decode(code);
};
frameworks.widgets.Result.prototype = {
	decode : function(code){
		this.valid = false;
		this.data = "";
		this.errors = {};
		this.attachments = [];
		this.source = "";
		if(code != null){
			if(code.hasOwnProperty("valid")){this.valid = code.valid;}
			if(code.hasOwnProperty("data")){this.data = code.data;}
			if(code.hasOwnProperty("errors")){this.errors = code.errors;}
			if(code.hasOwnProperty("attachment")){this.attachment = code.attachment;}
			if(code.hasOwnProperty("source")){this.source = code.source;}	
		}
	}
};


frameworks.widgets.ResultProxy = function(code){
	this.completeCallback = [];
	this.successCallBacks = [];
	this.errorCallBack = [];
	this.progressCallback = [];
};
frameworks.widgets.ResultProxy.prototype = {
	addCompleteCallback : function(callBack,args){
		this.completeCallback.push({"fun":callBack,"args":args});
	},
	addSuccessCallback : function(callBack,args){
		this.successCallBacks.push({"fun":callBack,"args":args});
	},
	addErrorCallback : function(callBack,args){
		this.errorCallBack.push({"fun":callBack,"args":args});
	},
	addProgressCallback : function(callBack,args){
		this.progressCallback.push({"fun":callBack,"args":args});
	},
	_setResultData : function(callbacks,result){
		var item;
		for(var i=0,k=callbacks.length;i<k;i++){
			item = callbacks[i];
			if(item["args"]){
				item["args"].push(result);
				item["fun"].apply(null,item["args"]);
			}else{
				item["fun"](result);
			}
		}
	},
	setSuccessData : function(result){
		this._setResultData(this.successCallBacks, result);
		this._setResultData(this.completeCallback, result);
	},
	setErrorData : function(result){
		this._setResultData(this.errorCallBack, result);
		this._setResultData(this.completeCallback, result);
	},
	setProgressData : function(data){
		this._setResultData(this.progressCallback, data);
	}
};


frameworks.widgets.BaseStackItem  = function(){
	frameworks.widgets.ResultProxy.call(this);
	this.stack = null;
	this.callBack = "ssssss";
};
frameworks.widgets.BaseStackItem.prototype = new frameworks.widgets.ResultProxy();
frameworks.widgets.BaseStackItem.prototype.__super = frameworks.widgets.ResultProxy;
frameworks.widgets.BaseStackItem.prototype.execute = function(){};
frameworks.widgets.BaseStackItem.prototype.setSuccessData = function(result){
	alert(frameworks.widgets.BaseStackItem.prototype.__super)
	alert(this.__super);
	this.__super.prototype.setSuccessData.call(null, result);
	this.stack.successItem(result);
	this.stack.completeItem(result);
};
frameworks.widgets.BaseStackItem.prototype.setErrorData = function(result){
	this.__super.prototype.setErrorData.call(this, result);
	this.stack.errorItem(result);
	this.stack.completeItem(result);
};
frameworks.widgets.BaseStackItem.prototype.setProgressData = function(data){
	this.__super.prototype.setProgressData.call(this, data);
	stack.progressItem(data);
};

frameworks.widgets.SenderManager = function(){
	this._oid = 0;
	this._watcherFunctions = new frameworks.widgets.Dictionary();
	this._initWatcherFunction = new frameworks.widgets.Dictionary();;
	this._changedSenders = new frameworks.widgets.Dictionary();
	this._changed = false;
	this._senderHasOrder = false;
};
frameworks.widgets.SenderManager.prototype = {
	senderChanged : function(sender){
		this._changedSenders.setData(sender,true);
		if(!this._changed){this._changed = true;}
	},
	addInitWatcherFunction : function(fun){
		this._initWatcherFunction.setData(fun,true);
		if(!this._changed){this._changed = true;}
	},
	regWatcherFunction : function(watchers,fun){
		this._oid ++;
		if(this._oid > 10000000){
			var tem = [];
			this._watcherFunctions.each(
				function(v,o,i){
					tem.push({order:v.order, fun:i});
				}
			);
			tem.sort(Util.SortNumericBy("order"));
			var list = this._watcherFunctions.list;
			for (var i=0,k=tem.length;i<k;i++){
				list[tem[i].fun].order = i;
			}
			i ++;
			this._oid = i;
		}
		this._watcherFunctions.setData(fun,{order:this._oid, watchers:watchers});
	},
	updateChanges : function(args){
		var changedSenders,updateFuns,arr,sender,watchers,changedSendersList,i,k,item,fun,tem;
		while(this._changed){
			trace("SenderManager=====updateChanges");
			this._changed = false;
			changedSenders = this._changedSenders;
			this._changedSenders = new frameworks.widgets.Dictionary();
			changedSendersList = [];
			if(this._senderHasOrder){
				arr = [];
				changedSenders.each(function(v,o,i){arr.push({order:o.levelID, value:o});});
				arr.sort(Util.SortNumericBy("order"));
				for(i=0,k=arr.length;i<k;i++){
					item = arr[i];
					changedSendersList.push(item.value);
				}
			}else{
				changedSenders.each(function(o,k,i){changedSendersList.push(k);});
			}
			arr = [];
			/*i = changedSendersList.length;
			while(i > 0){
			i--;
			ILayer(affectedList[i]).preSubmit();
			}*/
			for(i=0,k=changedSendersList.length;i<k;i++){
				sender = changedSendersList[i];
				if(this._changedSenders.getData(sender) !== undefined ){
					trace("delete repeat sender");
					this._changedSenders.delKey(sender);
				}
				sender.preVerify();
			}
			for(i=0,k=changedSendersList.length;i<k;i++){
				sender = changedSendersList[i];
				sender.frontVerify();
			}
			i = changedSendersList.length;
			while(i > 0){
				i--;
				changedSendersList[i].backVerify();
			}
			updateFuns = this._initWatcherFunction;
			this._initWatcherFunction = new frameworks.widgets.Dictionary();
			for(i=0,k=changedSendersList.length;i<k;i++){
				sender = changedSendersList[i];
				sender.sumbitChangedFuns(updateFuns);
			}
			arr = [];
			tem = this;
			updateFuns.each(function(v,o,i){
				item = tem._watcherFunctions.getData(o);
				arr.push({order:item.order,watchers:item.watchers,fun:o,value:v});
			});
			arr.sort(Util.SortNumericBy("order"));
			for(i=0,k=arr.length;i<k;i++){
				item = arr[i];
				item.fun.call(item.watchers, item.value);
			}
			for(i=0,k=changedSendersList.length;i<k;i++){
				sender = changedSendersList[i];
				sender.senderAllComplete();
			}
		}
	}
};



frameworks.widgets.BaseSender = function(manager){
	this._manager = manager;
	this._propertys = {"*":1};
	this._propertyAccess = {"*":1};
	this._propertysChanged = {};
	this._propertysChangedTemp = this._propertysChanged;
	this._propertyWatchers = {}
};
frameworks.widgets.BaseSender.prototype = {
	preVerify : function(){},
	frontVerify : function(){},
	backVerify : function(){},
	regProperty : function(property, value, access){
		if(this._propertys[property] !== undefined){throw new Error(property+"属性已经存在！");return;}
		if(access === 0 && !(value instanceof frameworks.widgets.BaseSender)){throw new Error(property+"锁定的属性必须为Sender！");return;}
		this._propertyAccess[property] = access;
		this._propertys[property] = value;
	},
	getProperty : function(property){
		return this._propertys[property];
	},
	setProperty : function(property, value){
		var propertyAccess = this._propertyAccess;
		var propertys = this._propertys;
		if(propertyAccess[property] !== -1 && propertys[property] !== value){
			if((typeof(value) === "number") && isNaN(value) && isNaN(propertys[property])){return false;}
			if(propertyAccess[property] === 0){
				propertys[property].setPropertysSource(value);
			}else{
				propertys[property] = value;
				this._propertysChanged[property] = true;
				if(!this._propertysChanged["*"]){
					this._propertysChanged["*"] = true;
					this._manager.senderChanged(this);
				}
			}
			return true;
		}else{
			return false;
		}
	},
	setPropertysSource : function(data){
		var propertyAccess = this._propertyAccess;
		for(var property in propertyAccess){
			if(propertyAccess[property]!==-1 && data.hasOwnProperty(property)){
				this.setProperty(property, data[property]);
			}
		}
	},
	addWatcher : function(watchers, property, fun){
		if(property == ""){property = "*";}
		if(this._propertyAccess[property] === 1){
			watchers.senderAdded(this, property, fun);
			this.watchersAdded(watchers, property, fun);
			this._manager.addInitWatcherFunction(fun);
		}else{
			throw new Error(property+"属性不允许改变！无效的侦听！");
		}
	},
	watchersAdded : function(watchers, property, fun){
		this._manager.regWatcherFunction(watchers,fun);
		var dictionary,dictionary2,Bool; 
		dictionary = this._propertyWatchers[property];
		if(dictionary === undefined){
			dictionary = new frameworks.widgets.Dictionary();
			this._propertyWatchers[property] = dictionary;
		}
		dictionary2 = dictionary.getData(watchers);
		if(dictionary2 === undefined){
			dictionary2 = new frameworks.widgets.Dictionary();
			dictionary.setData(watchers, dictionary2);
		}
		Bool = dictionary2.getData(fun);
		if(Bool === undefined){
			dictionary2.setData(fun, true);
		}
	},
	sumbitChangedFuns : function(changedFuns){
		var propertysChanged = this._propertysChanged;
		for(var property in propertysChanged){
			if(propertysChanged.hasOwnProperty(property)){
				var dictionary = this._propertyWatchers[property];
				if(dictionary != null){
					for(var key in dictionary.list){
						if(dictionary.list.hasOwnProperty(key)){
							var dictionary2 = dictionary.list[key];
							for(var key2 in dictionary2.list){
								if(dictionary2.list.hasOwnProperty(key2)){
									changedFuns.setData(dictionary2.mapping[key2],false);
								}
							}
						}	
					}
				}
			}
		}
		this._propertysChanged = {};
	},
	senderAllComplete : function(){
		this._propertysChangedTemp = this._propertysChanged;
	}
};


frameworks.widgets.Watchers = function(){
	this._senders = new frameworks.widgets.Dictionary();
};
frameworks.widgets.Watchers.prototype = {
	senderAdded : function(sender, property, fun){
		var object,dictionary,bool; 
		object = this._senders.getData(sender);
		if(object === undefined){
			object = {};
			this._senders.setData(sender, object);
		}
		dictionary = object[property];
		if(dictionary === undefined){
			dictionary = new frameworks.widgets.Dictionary();
			object[property] = dictionary;
		}
		bool = dictionary.getData(fun);
		if(bool === undefined){
			dictionary.setData(fun, true);
		}
	}
};

frameworks.widgets.Dictionary = function(){
	this.mapping = {};
	this.list = {};
};
frameworks.widgets.Dictionary.prototype = {
	setData : function(key, value){
		var keyID = key.getDictionaryID();
		this.list[keyID] = value;
		this.mapping[keyID] = key;
	},
	getData : function(key){
		var keyID = key.getDictionaryID();
		return this.list[keyID];
	},
	each : function(fun){
		var i,list = this.list,mapping = this.mapping;
		for(i in list){
			if(list.hasOwnProperty(i)){
				fun(list[i],mapping[i],i);
			}
		}
	},
	delKey : function(key){
		var keyID = key.getDictionaryID();
		delete this.list[keyID];
		delete this.mapping[keyID];
	}
};